function $(id){
    return document.getElementById(id);
}
window.onload = function () {
    findByItem(1)
}

/***
 * 动态分页查询
 * @param pageNum
 */
function findByItem(pageNum) {
    axios.get("/room/findByItem",{
        params:{
            pageNum:pageNum,
            address:$("address").value,
            price:$("startPrice").value,
            endPrice:$("endPrice").value,
        }
    }).then(resp => {
        var str =""
        let info =resp.data.list
        info.forEach(e =>{
            str +=`<tr><td>${e.address}</td><td>${e.price}</td><td>${e.createDate}</td><td><input type="button" value="删除" onclick="delRoom(${e.id})">
            <input type="button" value="修改" onclick="findById(${e.id})"></td></tr>`
            $("roomDate").innerHTML= str;
        });
        let pageN ="";
        for(let i=1;i<=resp.data.pages;i++){
            pageN +=`<a style="margin-left: 10px" href="javascript:findByItem(${i})">${i}</a>`
            $("pageDiv").innerHTML =pageN;
        }
    });

}

/***
 * 显示遮罩
 */
function showAddDiv() {
    $("addDiv").style.visibility="visible";
    $("addDiv").style.width = document.documentElement.clientWidth+"px";
    $("addDiv").style.height = document.documentElement.clientHeight+"px";
}

/***
 * 添加房间
 */
function addRoom(){
    axios.get("/room/add",{
        params:{
            address:$("addAddress").value,
            price:$("addPrice").value,
            createDate:$("addDate").value
        }
    }).then(resp =>{
        if(resp.data == "ok"){
            $("addDiv").style.visibility="hidden";
            findByItem(1)
            $("address").value=""
            $("addPrice").value=""
            $("addDate").value=""
        }
    });
}

/***
 * 按id删除房间
 * @param id 房间id
 */
function delRoom(id) {
   axios.get("/room/del",{
       params:{
           id:id
       }
   }).then(resp =>{
      if(resp.data == "ok"){
          findByItem(1)
      }
   });
}

/***
 * 显示
 * @param id
 */
function findById(id) {
   axios.get("/room/findById",{
       params:{
           id:id
       }
   }).then(resp =>{
       var  obj = resp.data;
       $("updateAddress").innerHTML=obj.address
       $("updatePrice").value = obj.price
       $("updateDate").innerHTML = obj.createDate
       $("roomId").value = obj.id

       $("updateDiv").style.visibility="visible";
       $("updateDiv").style.width = document.documentElement.clientWidth+"px";
       $("updateDiv").style.height = document.documentElement.clientHeight+"px";
   });
}

/***
 * 修改房间价格
 */
function updateRoom() {
    axios.get("/room/update",{
        params:{
            id:$("roomId").value,
            price:$("updatePrice").value
        }
    }).then(resp =>{
        if(resp.data == "ok"){
            findByItem(1)
            $("updateDiv").style.visibility="hidden";
        }
    });
}